import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/theme/gt_colors_v3.dart';

import '../../generated/l10n.dart';

/// @Title   弹出文字列表bottomsheet
/// @Author: bryce
/// @CreateDate: 2023年10月18日16:09:54
Future<int?> showGTBottomSheet(
  context, {
  required Widget child, // 子组件
  double? maxHeight, // bottomSheet最大高度
  double? minHeight, // bottomSheet最小高度
  double topPadding = 24, // child上边距
  double bottomPadding = 24, // child下边距，不是指cancel按钮下的边距
  double leftPadding = 16,
  double rightPadding = 16,
  double topRadius = 8.0, // bottomSheet上方圆角
  bool showBottomCancel = false, // 是否展示底部取消按钮
  bool showTopRightClose = false, // 是否展示右上角关闭按钮
  String? title,
}) async {
  // 最大高度
  double bottomSheetMaxHeight = maxHeight ?? (GTScreen.height * 0.75);
  double cancelHeight = showBottomCancel ? 44 : 0;
  double titleHeight = title != null ? 42 : 0;
  // child的最大高度：总的最大高度-标题高度-child上边距-child下边距-cancel高度
  double childMaxHeight = bottomSheetMaxHeight - titleHeight - topPadding - bottomPadding - cancelHeight;
  double childMinHeight = minHeight ?? 0 - titleHeight - topPadding - bottomPadding - cancelHeight;
  if (childMaxHeight < 0) childMinHeight = 40;
  if (childMinHeight < 0) childMinHeight = 0;

  return showModalBottomSheet<int>(
    // constraints: BoxConstraints(maxHeight: maxHeight),
    backgroundColor: Colors.transparent,
    barrierColor: const Color.fromRGBO(0, 0, 0, 0.5),
    isScrollControlled: true,
    context: context,
    builder: (BuildContext context) {
      return Wrap(
        children: [
          Stack(
            children: [
              Container(
                clipBehavior: Clip.antiAlias,
                decoration: BoxDecoration(
                  color: GTColorsV3.bg1.color,
                  borderRadius:
                      BorderRadius.only(topLeft: Radius.circular(topRadius), topRight: Radius.circular(topRadius)),
                ),
                padding: GTEdgeInsetsOnly(left: leftPadding, right: rightPadding),
                child: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [
                  // 设置标题
                  title != null
                      ? Container(
                          height: titleHeight,
                          alignment: Alignment.bottomLeft,
                          child: Text(
                            title,
                            style: gtTextStyleW600(color: GTColorsV3.text1.color, fontSize: 16),
                          ))
                      : Container(),
                  // child
                  Container(
                      margin: EdgeInsets.only(top: topPadding, bottom: bottomPadding),
                      constraints: BoxConstraints(maxHeight: childMaxHeight, minHeight: childMinHeight),
                      child: child),
                  // 设置Cancel按钮
                  showBottomCancel
                      ? GestureDetector(
                          onTap: () => Navigator.pop(context),
                          behavior: HitTestBehavior.opaque,
                          child: SafeArea(
                              child: Container(
                            height: cancelHeight,
                            alignment: Alignment.center,
                            child: Text(
                              LibUIKitS.of(context).Cancel,
                              style:
                                  TextStyle(color: GTColorsV3.text5.color, fontSize: 14, fontWeight: FontWeight.w600),
                            ),
                          )))
                      : Container()
                ]),
              ),
              // 设置右上角关闭
              showTopRightClose
                  ? Positioned(
                      top: 0,
                      right: 0,
                      child: GestureDetector(
                        onTap: () => Navigator.pop(context),
                        child: Padding(
                          padding: const EdgeInsets.all(16),
                          child: Icon(
                            GTIconFont.e841,
                            color: GTColorsV3.icon3.color,
                            size: 20,
                          ),
                        ),
                      ))
                  : Container()
            ],
          )
        ],
      );
    },
  );
}
